home *** CD-ROM | disk | FTP | other *** search
- /* WriteEPSF.c */
- /* Low level routines for buffering output */
- /* Copyright 1992, Gary D. McGath */
-
- #define BUFFSIZE 512
-
- void InitOutBuf(void);
- void OutputString(char *str, int theFile);
- void OutputChar(int ch,int theFile);
- void OutputNum(int val, int theFile);
- void FlushOutBuf(int theFile);
- static void OutputHexNibble(int nib, int theFile);
-
- static char buff[BUFFSIZE]; /* output buffer */
- static int buffoff; /* current offset into buffer */
-
- /* This must be called before doing any buffered I/O */
- void InitOutBuf()
- {
- buffoff = 0; /* make empty buffer */
- }
-
- /* Write a C string to the file */
- void OutputString(char *str, int theFile)
- {
- while (*str)
- OutputChar(*str++,theFile);
- }
-
- /* write a number to the file, in decimal, with a trailing space */
- void OutputNum(int val, int theFile)
- {
- int dvsr = 10000;
- int qtnt;
- int leadflag = 0; /* 0 if no significant digits yet */
- if (val < 0) {
- val = -val;
- OutputChar('-',theFile); /* take care of negative nos. */
- }
- while (dvsr > 0) {
- if (dvsr == 1)
- leadflag = 1; /* always do at least one digit */
- qtnt = val/dvsr;
- if (qtnt == 0) {
- if (leadflag) /* need to output a zero? */
- OutputChar('0',theFile);
- }
- else {
- OutputChar(qtnt + '0', theFile); /* output a digit */
- leadflag = 1; /* now we've seen a leading digit */
- }
- val = val - qtnt * dvsr; /* take the remainder */
- dvsr /= 10; /* down one decimal place */
- }
- OutputChar(' ', theFile); /* trailing space */
- }
-
- /* OutputDouble -- similar in concept to OutputNum. Doesn't handle huge
- or tiny numbers reasonably. */
- void OutputDouble(double val, int theFile)
- {
- long ival;
- long dvsr = 1000000;
- int qtnt;
- int leadflag = 0; /* 0 if no significant digits yet */
-
- if (val < 0.001 && val > -0.001) { /* special case for zero or almost */
- OutputNum(0, theFile);
- return;
- }
- ival = val * 1000;
- if (ival < 0) {
- ival = -ival;
- OutputChar('-',theFile); /* take care of negative nos. */
- }
- while (dvsr > 0) {
- if (dvsr == 100) {
- OutputChar ('.', theFile); /* the decimal point */
- leadflag = 1; /* always do zeroes in fraction */
- }
- qtnt = ival/dvsr;
- if (qtnt == 0) {
- if (leadflag) /* need to output a zero? */
- OutputChar('0',theFile);
- }
- else {
- OutputChar(qtnt + '0', theFile); /* output a digit */
- leadflag = 1; /* now we've seen a leading digit */
- }
- ival = ival - qtnt * dvsr; /* take the remainder */
- dvsr /= 10; /* down one decimal place */
- if (ival == 0 && dvsr <= 100)
- break; /* leave out trailing non-significant zeroes */
- }
- OutputChar(' ', theFile); /* trailing space */
-
- }
-
- void OutputHex(int ch, int theFile)
- {
- OutputHexNibble(ch >> 4, theFile);
- OutputHexNibble(ch, theFile);
- }
-
- static void OutputHexNibble(int nib, int theFile)
- {
- nib &= 0XF;
- if (nib > 9) /* A-F */
- OutputChar(nib - 10 + 'A', theFile);
- else OutputChar(nib + '0', theFile);
- }
-
- /* Write one character to the file */
- void OutputChar(int ch, int theFile)
- {
- long count = BUFFSIZE;
- buff[buffoff++] = ch;
- if (buffoff >= BUFFSIZE) {
- FSWrite(theFile,&count, buff);
- buffoff = 0;
- }
- }
-
- /* Write out whatever is left in the buffer. This must be called before
- closing. */
- void FlushOutBuf(int theFile)
- {
- long count;
- count = buffoff;
- FSWrite(theFile,&count, buff);
- }
-